namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerBasicInfoPageForSearch : Page
    {
        protected TextBox CustomerDepartTxb;
        protected TextBox CustomerEmailTxb;
        protected TextBox CustomerGenderTxb;
        protected TextBox CustomerKindTxb;
        protected DropDownList CustomerLevelDdl;
        protected TextBox CustomerNameTxb;
        protected ListBox CustomerProductLB;
        protected TextBox CustomerTitleTxb;
        protected Image iCustomerInfo;
        protected Label lbCustomerDepart;
        protected Label lbCustomerEmail;
        protected Label lbCustomerGender;
        protected Label lbCustomerKind;
        protected Label lbCustomerLevel;
        protected Label lbCustomerName;
        protected Label lbCustomerProduct;
        protected Label lbCustomerTitle;
        protected UserPrincipal Principal = null;

        private void FillPage()
        {
            Guid id = new Guid(base.Request.QueryString["CustomerId"]);
            CustomerBasicInfo customerBasicInfo = ManageCustomer.GetCustomerBasicInfoViaId(id);
            this.CustomerNameTxb.Text = customerBasicInfo.Name;
            this.CustomerGenderTxb.Text = customerBasicInfo.Gender;
            this.CustomerTitleTxb.Text = customerBasicInfo.Title;
            this.CustomerDepartTxb.Text = customerBasicInfo.Depart;
            this.CustomerEmailTxb.Text = customerBasicInfo.CustomerEmail;
            for (int i = 0; i < this.CustomerLevelDdl.Items.Count; i++)
            {
                if (this.CustomerLevelDdl.Items[i].Value == customerBasicInfo.Level.ToString())
                {
                    this.CustomerLevelDdl.SelectedIndex = i;
                }
            }
            string language = this.Session["language"].ToString();
            if (language == "cn")
            {
                this.CustomerKindTxb.Text = ManageCustomerDef.GetDefViaId(customerBasicInfo.CustomerDefId).DefName;
            }
            else if (language == "en")
            {
                this.CustomerKindTxb.Text = ManageCustomerDef.GetDefViaId(customerBasicInfo.CustomerDefId).DefNameEn;
            }
            Product[] productEdit = ManageProduct.GetProductAryViaId(customerBasicInfo.CustomerId);
            for (int i = 0; i < productEdit.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                switch (language)
                {
                    case "cn":
                        tmpListItem.Text = productEdit[i].ProductNameCn;
                        break;

                    case "en":
                        tmpListItem.Text = productEdit[i].ProductNameEn;
                        break;
                }
                tmpListItem.Value = productEdit[i].ProductId.ToString();
                this.CustomerProductLB.Items.Add(tmpListItem);
            }
        }

        private void InitializeComponent()
        {
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.CustomerLevelDdl.Items.Clear();
            for (int i = 1; i <= 5; i++)
            {
                string tmpStr = "Level " + i.ToString();
                ListItem tmpItem = new ListItem();
                tmpItem.Text = tmpStr;
                tmpItem.Value = i.ToString();
                this.CustomerLevelDdl.Items.Add(tmpItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeA == 0)
            {
                this.InitPage();
                this.FillPage();
            }
            else if (this.Principal.OwnRole.TypeA == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbCustomerName.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerName", language, path);
            this.lbCustomerLevel.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerLevel", language, path);
            this.lbCustomerGender.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerGender", language, path);
            this.lbCustomerTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerTitle", language, path);
            this.lbCustomerDepart.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerDepart", language, path);
            this.lbCustomerEmail.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerEmail", language, path);
            this.lbCustomerKind.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerKind", language, path);
            this.lbCustomerProduct.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerBasicInfoPageForSearch", "lbCustomerProduct", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iCustomerInfo.ImageUrl = "../images/title_customerinfo.jpg";
                    break;

                case "en":
                    this.iCustomerInfo.ImageUrl = "../images/title_customerinfo.jpg";
                    break;
            }
        }
    }
}

